<?php

namespace app\utils;

use ReflectionClass;
use think\Request;

class Utils
{
    public static function isEmptyForArray($array)
    {
        return isset($array) && sizeof($array) > 0;
    }

    public static function toStringBoolean($isyes)
    {
        if ($isyes) {
            return 'true';
        } else {
            return 'false';
        }
    }

    public static function ok($data, $total = 0)
    {
        return json([
            "success" => true,
            "data" => $data,
            "code" => 200,
            "total" => $total
        ]);
    }

    public static function failRes(string $message, int $code = 500)
    {
        return json([
            "success" => false,
            "message" => $message,
            "code" => $code
        ], $code);
    }

    public static function isNull($obj)
    {
        return (!isset($obj)) || (is_null($obj));
    }

    public static function copyProperties($clazz, $data)
    {
        $refObj = new ReflectionClass($clazz);
        $ref_props = $refObj->getProperties();
        $newObject = $refObj->newInstance();
        for ($i = 0; $i < count($ref_props); $i++) {
            $key = $ref_props[$i]->name;
            $reflectionProperty = $refObj->getProperty($key);
            if (isset($data) && isset($data[$key])) {
                $reflectionProperty->setValue($newObject, $data[$key]);
            }
        }
        return $newObject;
    }

    static function dateyyyymmddhhmmss($date)
    {
        return date("y-m-d H:i:s", $date);
    }

    static function dateyyyymmdd($date)
    {
        return date("y-m-d", $date);
    }

    static function fdate($time, $format)
    {
        return date($format, $time);
    }

    static function isNullPlus($obj, $propertyKey)
    {
        return empty($obj) ||
            (!isset($obj[$propertyKey])) ||
            empty($obj[$propertyKey]) ||
            is_null($obj[$propertyKey]);
    }

    static function isNotNull($obj, $propertyKey)
    {
        return !self::isNullPlus($obj, $propertyKey);
    }

    static function getWithDefault($obj, $propertyKey, $defaultValue)
    {
        return $obj[$propertyKey] ?? $defaultValue;
    }

    static function toHump(string $str)
    {
        $str = preg_replace_callback('/([-_]+([a-z]{1}))/i', function ($matches) {
            return strtoupper($matches[2]);
        }, $str);
        return $str;
    }

    /*
    * 驼峰转下划线
    */
    static function toLine($str)
    {
        $str = str_replace("_", "", $str);
        $str = preg_replace_callback('/([A-Z]{1})/', function ($matches) {
            return '_' . strtolower($matches[0]);
        }, $str);
        return ltrim($str, "_");
    }

    static function getUserInfoName(Request $request)
    {
        $userinfo = $request->UserInfo;
        if (!isset($userinfo)) {
            return '';
        }
        return $userinfo['username'] ?? '';
    }

    static function getUserInfoId(Request $request)
    {
        $userinfo = $request->UserInfo;
        if (!isset($userinfo)) {
            return '';
        }
        return $userinfo['id'] ?? '';
    }

    static function map($array, \Closure $callback)
    {
        return array_map($callback, $array);
    }


    static function toHumpForObject($obj)
    {
        if (empty($obj)) {
            return [];
        }
        $newItem = [];
        foreach ($obj as $key => $value) {
            $newItem[self::toHump($key)] = $value;
        }

        return $newItem;
    }

    static function toHumpForArray($array)
    {
        if (empty($array)) {
            return [];
        }

        $newArray = [];
        foreach ($array as $item) {
            $newItem = [];
            foreach ($item as $key => $value) {
                $newItem[self::toHump($key)] = $value;
            }
            array_push($newArray, $newItem);
        }

        return $newArray;
    }

    public static function getValue($params, $key,$defaultValue=null)
    {
        if (!isset($params)) {
            return $defaultValue;
        }
        if(array_key_exists($key,$params)){
            return $params[$key];
        }
        return $defaultValue;
    }
}



